//
//  ZxFmHomeLiveViewController.m
//  XiMaFM
//
//  Created by fanzhuxian on 2017/9/3.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ZxFmHomeLiveViewController.h"

#import "ZxFmLiveViewModel.h"
#import "ZxFmLiveHeaderViewModel.h"
#import "ZxFmLiveCollectionViewCell.h"

#import <SDCycleScrollView.h>
#import "CXRefreshStateHeader.h"

static NSString * const kCell = @"cellID";
static NSString * const hCell = @"UICollectionViewHeader";

@interface ZxFmHomeLiveViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,SDCycleScrollViewDelegate>

@property (nonatomic, strong) ZxFmLiveViewModel * liveModel;

@property (nonatomic, strong) ZxFmLiveHeaderViewModel * headerLiveModel;

@property (nonatomic, strong) UICollectionView * collectionView;

//@property (nonatomic, strong) NSMutableArray * dataArray;

@property (nonatomic, strong) NSString * type;

@property (nonatomic, assign) NSInteger categoryID;

@property (nonatomic, strong) UIImageView * img;

@end

@implementation ZxFmHomeLiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.collectionView.backgroundColor = [UIColor colorWithRed:239 / 255.0  green:243 / 255.0  blue:247 / 255.0 alpha:1];
    
    [self.collectionView.mj_header beginRefreshing];
//    [self netWorking];
}

#pragma mark - headView 创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:hCell forIndexPath:indexPath];
    NSMutableArray * imageUrl = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < self.headerLiveModel.focusImgNumber; i++) {
       
        NSString * str = [NSString stringWithFormat:@"%@", [self.headerLiveModel focusImgURLForIndex:i]];
        [imageUrl addObject:str];
        
        NSLog(@"%@", str);
    }
    
    SDCycleScrollView * scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT / 4) imageNamesGroup:imageUrl];
    
    scrollView.layer.masksToBounds = YES;
    scrollView.layer.cornerRadius = 4;
    scrollView.delegate = self;
    scrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    scrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [headerView addSubview:scrollView];
    
    return headerView;
}

#pragma mark - 图片点击事件
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    NSLog(@"%ld",(long)index);
}

// 设置头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {

    if (section == 0) {
        return CGSizeMake(DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT / 4);
        
    } else {
        return  CGSizeMake(0, 0);
        
    }
}


#pragma mark - collection dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.liveModel.section;
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    ZxFmLiveCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCell forIndexPath:indexPath];
    
//    cell.model = self.dataArray[indexPath.row];
    [cell.picImgView sd_setImageWithURL:[self.liveModel coverURLForSection:indexPath.section index:indexPath.item] placeholderImage:[UIImage imageNamed:@"cell_bg_noData_2"]];
    cell.nicknameLabel.text = [self.liveModel nicknameForSection:indexPath.section index:indexPath.item];
   
    cell.nameLabel.text = [self.liveModel nameForSection:indexPath.section index:indexPath.item];
    
    [cell.categoryNameTag setTitle:[NSString stringWithFormat:@"%ld", [self.liveModel playCountForSection:indexPath.section index:indexPath.item]] forState:UIControlStateNormal];

    [cell.playCountLabel setTitle:[NSString stringWithFormat:@"%ld",[self.liveModel playCountForSection:indexPath.section index:indexPath.item]] forState:UIControlStateNormal];

    
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

//设置cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(DEVICE_SCREEN_WIDTH / 2 - 20, DEVICE_SCREEN_WIDTH / 2 + 30);
    
}


#pragma mark - collectionCell点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"点击了%ld",indexPath.row);
    
}



- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        flowLayout.minimumLineSpacing = 10.0f;
        flowLayout.minimumInteritemSpacing = 1;
        flowLayout.headerReferenceSize = CGSizeMake(DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT / 4);
        flowLayout.sectionInset = UIEdgeInsetsMake(20, 10, 3, 10);
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT - 35 - 49 - 64) collectionViewLayout:flowLayout];
        [self.view addSubview:collectionView];
        
        collectionView.delegate = self;
        collectionView.dataSource = self;
        
        collectionView.backgroundColor = [UIColor whiteColor];
        
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZxFmLiveCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:kCell];
        
        collectionView.bounces = YES;
        
        // 注册头视图
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:hCell];
        
        self.collectionView = collectionView;
        
        // 上拉刷新
        collectionView.mj_header = [CXRefreshStateHeader headerWithRefreshingBlock:^{
            
            [self.headerLiveModel getDataCompletionHandle:^(NSError *error) {
                [self.collectionView.mj_header endRefreshing];
                [self.collectionView reloadData];
            }];
            
            [self.liveModel getDataCompletionHandle:^(NSError *error) {
                [self.collectionView.mj_header endRefreshing];
                [self.collectionView reloadData];
            }];
        }];
        
    }
    
    return _collectionView;
}

#pragma mark - 懒加载
- (ZxFmLiveViewModel *)liveModel {

    if (!_liveModel) {
        _liveModel = [[ZxFmLiveViewModel alloc] init];
    }
    return  _liveModel;
}

- (ZxFmLiveHeaderViewModel *)headerLiveModel {

    if (!_headerLiveModel) {
        _headerLiveModel = [[ZxFmLiveHeaderViewModel alloc] init];
    }
    return _headerLiveModel;
}




@end
